package com.jy.horny.function

import com.framework.network.remote.NetworkObserver
import com.framework.network.remote.endRequest
import com.framework.network.remote.error
import com.framework.network.remote.requestObserver
import com.framework.network.remote.startRequest
import com.framework.network.remote.success
import com.jy.horny.base.BaseVMViewModel
import com.jy.horny.base.HttpRequestListener
import com.jy.horny.base.Request
import com.jy.horny.model.IMTokenModel
import com.jy.horny.model.PolicyModel
import com.jy.horny.model.ServerUserInfo
import com.jy.horny.model.UserInfoBaseModel

class MainViewModel : BaseVMViewModel() {
    fun getIMToken(listener: HttpRequestListener<IMTokenModel>) {
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postIMToken(map)?.requestObserver(
                    NetworkObserver<IMTokenModel>().startRequest {
                        showLoading()
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        hideLoading()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }
    fun getUserInfoBase(listener: HttpRequestListener<UserInfoBaseModel>) {
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postUserInfoBase(map)?.requestObserver(
                    NetworkObserver<UserInfoBaseModel>().startRequest {
                        showLoading()
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        hideLoading()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }
    fun getPolicy(listener: HttpRequestListener<PolicyModel>){
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postPolicy(map)?.requestObserver(
                    NetworkObserver<PolicyModel>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }
    fun getServer(listener: HttpRequestListener<List<ServerUserInfo>>){
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postServerUserInfo(map)?.requestObserver(
                    NetworkObserver<List<ServerUserInfo>>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }


}